using System;
using System.Collections.Generic;
using System.Text;

namespace Deepcode.Flux.Core.Systems.CMS.DataTaxonomy
{
	/// <summary>
	/// Represents a collection of taxonomy selections against a node.
	/// </summary>
	public class TaxonomySelections 
	{
		// Stores the list of keys
		protected List<int> _Keys = new List<int>();

		/// <summary>
		/// Returns an array of integer keys representing the 
		/// taxonomic ID's associated with an item of content.
		/// </summary>
		public int[] Keys { get { return _Keys.ToArray(); } }

		/// <summary>
		/// Adds the specified ID into the taxonomy selection
		/// </summary>
		/// <param name="ID"></param>
		public void Add(int ID)
		{
			if( _Keys.Contains( ID )) return;
			_Keys.Add( ID );
		}

		/// <summary>
		/// Finds the ID for the specified data taxonomy path 
		/// and adds it into the taxonomy selection. Will also
		/// ensure all parent taxonomy items are included.
		/// </summary>
		/// <param name="Path"></param>
		public void Add(string Path)
		{
			DataTaxonomyTree t = new DataTaxonomyTree();
			DataTaxonomyNode n = t.GetPath( Path );
			if (n != null)
			{
				// Add in all the parents
				DataTaxonomyNode trav = n.Parent;
				while (trav != null)
				{
					Add( trav.PK_ID );
					trav = trav.Parent;
				}
				// Finally add this ID in
				Add(n.PK_ID);
			}
		}

		/// <summary>
		/// Removes the specified ID from the taxonomy selection
		/// </summary>
		/// <param name="ID"></param>
		public void Remove(int ID)
		{
			_Keys.Remove( ID );
		}

		/// <summary>
		/// Finds the ID for the specified data taxonomy path
		/// and removes it from the taxonomy selection
		/// </summary>
		/// <param name="Path"></param>
		public void Remove(string Path)
		{
			DataTaxonomyTree t = new DataTaxonomyTree();
			DataTaxonomyNode n = t.GetPath( Path );
			if (n != null)
			{
				RemoveChildren( n );
				Remove(n.PK_ID);
			}
		}
		/// <summary>
		/// Used internally by the remove (Path) function
		/// to remove any children also.
		/// </summary>
		/// <param name="ID"></param>
		private void RemoveChildren(DataTaxonomyNode parent)
		{
			foreach (DataTaxonomyNode n in parent.Children)
			{
				RemoveChildren( n );
				Remove( n.PK_ID );
			}
		}
		/// <summary>
		/// Determines if the specified ID is already in this list
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public bool Contains(int ID)
		{
			return _Keys.Contains( ID );
		}

		/// <summary>
		/// Looks up the ID for the specified path and checks
		/// if it is already in this list.
		/// </summary>
		/// <param name="Path"></param>
		/// <returns></returns>
		public bool Contains(string Path)
		{
			DataTaxonomyTree t = new DataTaxonomyTree();
			DataTaxonomyNode n = t.GetPath( Path );
			if( n == null ) return false;
			return Contains( n.PK_ID );
		}

		/// <summary>
		/// Clears the list of selected ID's
		/// </summary>
		public void Clear()
		{
			_Keys.Clear();
		}
	}
}
